<?php
class Controller_BoFile extends Controller_Boprepare
{
    /**
     * 文件信息(Create)
     */
    public function actionUploadFile()
    {
        $FP = (isset($_FILES) && isset($_POST)) ? TRUE : FALSE; // 判读是否有上传的文件
        $Pctgry_id    = isset($_POST['ctgry_id'])    ? $_POST['ctgry_id']    : NULL;
        $Pdescription = isset($_POST['description']) ? $_POST['description'] : NULL;

        $note = '请上传文件';
        if($Pctgry_id && isset($_FILES['f']))
        {
            /**
             * 定义允许上传的文件类型
             * 可在数据库修改
             * 'jpg,png,gif, doc,docx,ppt,pptx,xsl,xslx, txt,pdf, rar,zip,tgz'
             */
            $allowExts = Q::ini('appini/uploadAllowExts');

            /**
             * NOTICE : 该值与PHP设置 {php.ini} 有关
             * 定义允许上传的最大尺寸
             * 可在数据库修改
             * 10 * 1024 * 1024 (10M)
             */
            $maxSize   = Q::ini('appini/uploadMaxSize');

            /**
             * 定义允许 课件文件夹
             * '../upload_tdoc'
             */
            $uploadDir = Q::ini('appini/uploadDir');

            $uploader  = new Helper_Uploader();
            $file      = $uploader->file('f');

            $fileid    = md5($_SERVER['REQUEST_TIME'] . $file->filename());
            $filename  = urlencode($fileid . '.' . strtolower($file->extname()));
            $file->move($uploadDir."/{$filename}");

            $newFileRecord = new File(
                array(
                    'path'        => $uploadDir."/{$filename}",
                    'filename'    => $file->filename(),
                    'type'        => $file->extname(),
                    'size'        => round($file->filesize()/1024/1024, 2).'M',
                    'category_id' => $Pctgry_id,
                    'description' => $Pdescription,
                    'created'     => $_SERVER['REQUEST_TIME'],
                )
            );
            $newFileRecord->save();

            $note = '文件'.$file->filename().'上传成功！';
        }

        $this->_view['note'] = $note;
    }
    /**
     * 文件信息(Read)
     */
    public function actionListFile()
    {
        if(!isset($_GET['ctgry_id']))
        {
            $_GET['ctgry_id'] = 3;
        }

        $this->_view['cssfile'] = 'bofile.css';
    }
    /**
     * 文件信息(Update)
     */
    public function actionEditFile()
    {
        $Pfile_id     = isset($_POST['file_id'])     ? $_POST['file_id']     : NULL;
        $Pctgry_id    = isset($_POST['ctgry_id'])    ? $_POST['ctgry_id']    : NULL;
        $Pdescription = isset($_POST['description']) ? $_POST['description'] : NULL;

        if($Pfile_id)
        {
            $file = File::find('file_id = ?', $Pfile_id)->query();
            $file->category_id = $Pctgry_id;
            $file->description = $Pdescription;
            $file->save();

            return $this->_forward('bofile/listfile');
        }
    }
    /**
     * 文件信息(Delete)
     */
    public function actionDeleteFile()
    {
        $Gfile_id = isset($_GET['file_id']) ? $_GET['file_id'] : NULL;
        $file = File::find('file_id = ?', $Gfile_id)->query();

        if($Gfile_id && $file->file_id != '')
        {
            $file->destroy();
        }

        return $this->_forward('bofile/listfile');
    }

    /**
     * 文件分类信息(Create, Read)
     */
    public function actionAddFileCtgry()
    {
        $Pnew_ctgry = isset($_POST['new_ctgry']) ? $_POST['new_ctgry'] : NULL;

        if($Pnew_ctgry && FileCategory::find('category = ?', $Pnew_ctgry)->query()->id == '')
        {
            $newFileCategoryRecord = new FileCategory(
                array(
                    'haschild'  => 0,
                    'category'  => $Pnew_ctgry,
                    'parent_id' => 3,
            ));
            $newFileCategoryRecord->save();
        }

        $this->_view['cssfile'] = 'bofile.css';
    }
    /**
     * 文件分类信息(Update)
     */
    public function actionEditFileCtgry()
    {
        $Pid       = isset($_POST['id'])       ? $_POST['id']       : NULL;
        $Pcategory = isset($_POST['category']) ? $_POST['category'] : NULL;

        if($Pid && $Pcategory)
        {
            $fileCtgry = FileCategory::find('id = ?', $Pid)->query();
            $fileCtgry->category = $Pcategory;
            $fileCtgry->save();
            
            return $this->_forward('bofile/addfilectgry');
        }
    }
    /**
     * 文件分类信息(Delete)
     * 有相关文件关联的分类不能删除
     */
    public function actionDeleteFileCtgry()
    {
        $Gid = isset($_GET['id']) ? $_GET['id'] : NULL;

        if($Gid && File::find('category_id = ?', $Gid)->query()->file_id == '')
        {
            FileCategory::meta()->deleteWhere('id = ? AND haschild != 1', $Gid);
        }

        return $this->_forward('bofile/addfilectgry');
    }
}